GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 92186c...418fbb )
by Florian
01:30
created

Okapi.setupSites   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
c 1
b 1
f 0
nc 3
nop 0
dl 0
loc 60
rs 9.5555

1 Function

Rating   Name   Duplication   Size   Complexity  
B 0 27 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*jslint
2
  indent: 4
3
*/
4
5
/*global
6
  $, google, mytrans, window, Cookies, Coordinates, get_cookie_string
7
*/
8
9
10
var Okapi = {};
11
Okapi.m_map = null;
12
Okapi.m_sites = null;
13
Okapi.m_ready = false;
14
Okapi.m_showCache = null;
15
Okapi.m_enabled = false;
16
Okapi.m_popup = null;
17
Okapi.m_marker = null;
18
Okapi.m_icons = null;
19
Okapi.m_timer = null;
20
21
22
Okapi.init = function (themap) {
23
    'use strict';
24
25
    this.m_map = themap;
26
    var self = this;
27
28
    google.maps.event.addListener(this.m_map, "center_changed", function () {
29
        self.scheduleLoad();
30
    });
31
    google.maps.event.addListener(this.m_map, "zoom_changed", function () {
32
        self.scheduleLoad();
33
    });
34
};
35
36
37
Okapi.parseLocation = function (s) {
38
    'use strict';
39
40
    var loc = s.split("|"),
41
        lat = parseFloat(loc[0]),
42
        lng = parseFloat(loc[1]);
43
44
    if (Coordinates.valid(lat, lng)) {
45
        return new google.maps.LatLng(lat, lng);
46
    }
47
48
    return null;
49
};
50
51
52
Okapi.setShowCache = function (code) {
53
    'use strict';
54
55
    this.m_showCache = code;
56
};
57
58
59
Okapi.setupSites = function () {
60
    'use strict';
61
62
    if (this.m_sites) {
63
        return;
64
    }
65
66
    var self = this,
67
        main_url = "http://www.opencaching.pl/okapi/services/apisrv/installations",
68
        keys = {
69
            "Opencaching.DE" : "YSqPufH82encfJ67ZxV2",
70
            "Opencaching.PL" : "jhRyc6rGmT6XEvxva29B",
71
            "Opencaching.NL" : "gcwaesuq3REu8RtCgLDj",
72
            "Opencaching.US" : "GvgyCMvwfH42GqJGL494",
73
            "Opencaching.ORG.UK" : "7t7VfpkCd4HuxPabfbHd",
74
            "Opencaching.RO" : "gqSWmVJhZGDwc4sRhyy7"
75
        },
76
        prefixes = {
77
            "Opencaching.DE" : "OC",
78
            "Opencaching.PL" : "OP",
79
            "Opencaching.NL" : "OB",
80
            "Opencaching.US" : "OU",
81
            "Opencaching.ORG.UK" : "OK",
82
            "Opencaching.RO" : "OR"
83
        };
84
85
    this.m_sites = [];
86
87
    $.ajax({
88
        url: main_url,
89
        dataType: 'json',
90
        success: function (response) {
91
            response.map(function (site) {
92
                if (keys[site.site_name] !== undefined) {
93
                    //console.log("adding OC site: " + site.site_name);
94
                    self.m_sites.push({
95
                        siteid: self.m_sites.length,
96
                        name: site.site_name,
97
                        site_url: site.site_url,
98
                        url: site.okapi_base_url,
99
                        prefix: prefixes[site.site_name],
100
                        key: keys[site.site_name],
101
                        ignore_user: null,
102
                        markers: {},
103
                        finished: true
104
                    });
105
                }
106
            });
107
108
            self.m_ready = true;
109
            if (self.m_enabled) {
110
                self.scheduleLoad(true);
111
            }
112
            if (self.m_showCache && self.m_showCache !== "") {
113
                self.centerMap(self.m_showCache);
114
                self.m_showCache = null;
115
            }
116
        }
117
    });
118
};
119
120
121
Okapi.setupIcons = function () {
122
    'use strict';
123
124
    if (this.m_icons) {
125
        return;
126
    }
127
128
    this.m_icons = {
129
        "Other": new google.maps.MarkerImage("img/cachetype-1.png"),
130
        "Traditional": new google.maps.MarkerImage("img/cachetype-2.png"),
131
        "Multi": new google.maps.MarkerImage("img/cachetype-3.png"),
132
        "Virtual": new google.maps.MarkerImage("img/cachetype-4.png"),
133
        "Webcam": new google.maps.MarkerImage("img/cachetype-5.png"),
134
        "Event": new google.maps.MarkerImage("img/cachetype-6.png"),
135
        "Quiz": new google.maps.MarkerImage("img/cachetype-7.png"),
136
        "Math/Physics": new google.maps.MarkerImage("img/cachetype-8.png"),
137
        "Moving": new google.maps.MarkerImage("img/cachetype-9.png"),
138
        "Drive-In": new google.maps.MarkerImage("img/cachetype-10.png")
139
    };
140
};
141
142
143
Okapi.icon = function (type) {
144
    'use strict';
145
146
    if (this.m_icons[type] !== undefined) {
147
        return this.m_icons[type];
148
    }
149
150
    return this.m_icons.Other;
151
};
152
153
154
Okapi.guessSiteId = function (code) {
155
    'use strict';
156
157
    code = code.toUpperCase();
158
    var siteid;
159
    for (siteid = 0; siteid < this.m_sites.length; siteid += 1) {
160
        if (code.startsWith(this.m_sites[siteid].prefix)) {
161
            return siteid;
162
        }
163
    }
164
165
    return -1;
166
};
167
168
169
Okapi.centerMap = function (code) {
170
    'use strict';
171
172
    if (!this.m_ready) {
173
        //console.log("okapi not ready");
174
        return;
175
    }
176
177
    var siteid = this.guessSiteId(code);
178
    if (siteid < 0) {
179
        //console.log("bad code. cannot determine okapi site");
180
        return;
181
    }
182
183
    this.showPopup(null, code.toUpperCase(), siteid);
184
};
185
186
187
Okapi.createPopupContent = function (code, response) {
188
    'use strict';
189
190
    var content =
191
        '<a href="' + response.url + '" target="_blank">' + code + ' <b>' + response.name + '</b></a><br />'
192
        + '<table>'
193
        + '<tr><td>' + mytrans("geocache.owner") + '</td><td>' + '<a href="' + response.owner.profile_url + '" target="_blank"><b>' + response.owner.username + '</b></a></td></tr>'
194
        + '<tr><td>' + mytrans("geocache.type") + '</td><td>' + response.type + '</td></tr>'
195
        + '<tr><td>' + mytrans("geocache.size") + '</td><td>' + response.size2 + '</td></tr>'
196
        + '<tr><td>' + mytrans("geocache.status") + '</td><td>' + response.status + '</td></tr>'
197
        + '<tr><td>' + mytrans("geocache.difficulty") + '</td><td>' + response.difficulty + '/5</td></tr>'
198
        + '<tr><td>' + mytrans("geocache.terrain") + '</td><td>' + response.terrain + '/5</td></tr>'
199
        + '<tr><td>' + mytrans("geocache.finds") + '</td><td>' + response.founds + '</td></tr>'
200
        + '</table>';
201
    return content;
202
};
203
204
205
Okapi.showPopup = function (m, code, siteid) {
206
    'use strict';
207
208
    if (!this.m_popup) {
209
        this.m_popup = new google.maps.InfoWindow();
210
    }
211
212
    var self = this,
213
        site = this.m_sites[siteid];
214
215
    $.ajax({
216
        url: site.url + 'services/caches/geocache',
217
        dataType: 'json',
218
        data: {
219
            'consumer_key': site.key,
220
            'cache_code': code,
221
            'fields' : 'name|type|status|url|owner|founds|size2|difficulty|terrain|location'
222
        },
223
        success: function (response) {
224
            var coords = self.parseLocation(response.location);
225
            self.m_map.setCenter(coords);
226
227
            if (!m) {
228
                m = new google.maps.Marker({
229
                    position: coords,
230
                    map: self.m_map,
231
                    icon: self.icon(response.type)
232
                });
233
                if (self.m_maker) {
234
                    self.m_marker.setMap(null);
235
                }
236
                self.registerPopup(m, code, siteid);
237
                self.m_marker = m;
238
            }
239
240
            self.m_popup.setContent(self.createPopupContent(code, response));
241
            self.m_popup.open(self.m_map, m);
242
        }
243
    });
244
};
245
246
247
Okapi.registerPopup = function (m, code, siteid) {
248
    'use strict';
249
250
    if (!this.m_ready) {
251
        return;
252
    }
253
254
    var self = this;
255
256
    google.maps.event.addListener(m, 'click', function () {
257
        self.showPopup(m, code, siteid);
258
    });
259
};
260
261
262
Okapi.removeMarkers = function () {
263
    'use strict';
264
265
    if (!this.m_ready) {
266
        return;
267
    }
268
269
    var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
270
    this.m_sites.map(function (site) {
271
        var key;
272
        for (key in site.markers) {
273
            if (!site.markers.hasOwnProperty(key)) {
274
                continue;
275
            }
276
            site.markers[key].setMap(null);
277
        }
278
        site.markers = {};
279
    });
280
281
    if (this.m_marker) {
282
        this.m_marker.setMap(null);
283
        delete this.m_marker;
284
        this.m_marker = null;
285
    }
286
};
287
288
289
Okapi.loadBboxSite = function (siteid) {
290
    'use strict';
291
292
    if (!this.m_ready) {
293
        return;
294
    }
295
296
    var self = this,
297
        site = this.m_sites[siteid],
298
        b,
299
        bbox;
300
301
    if (!this.m_enabled) {
302
        site.finished = true;
303
        return;
304
    }
305
306
    if (!site.finished) {
307
        return;
308
    }
309
310
    site.finished = false;
311
312
    b = this.m_map.getBounds();
313
    bbox = b.getSouthWest().lat() + "|" + b.getSouthWest().lng() + "|" + b.getNorthEast().lat() + "|" + b.getNorthEast().lng();
314
315
    $.ajax({
316
        url: site.url + 'services/caches/shortcuts/search_and_retrieve',
317
        dataType: 'json',
318
        data: {
319
            'consumer_key': site.key,
320
            'search_method': 'services/caches/search/bbox',
321
            'search_params': '{"bbox" : "' + bbox + '", "limit" : "500"}',
322
            'retr_method': 'services/caches/geocaches',
323
            'retr_params': '{"fields": "code|name|location|type|status|url"}',
324
            'wrap': 'false'
325
        },
326
        success: function (response) {
327
            var addedCaches = {},
328
                cache,
329
                code;
330
331
            for (code in response) {
332
                if (!response.hasOwnProperty(code)) {
333
                    continue;
334
                }
335
336
                cache = response[code];
337
                if (cache.status !== "Available") {
338
                    continue;
339
                }
340
341
                addedCaches[cache.code] = true;
342
                if (site.markers[cache.code] !== undefined) {
343
                    continue;
344
                }
345
346
                site.markers[cache.code] = new google.maps.Marker({
347
                    position: self.parseLocation(cache.location),
348
                    map: self.m_map,
349
                    icon: self.icon(cache.type)
350
                });
351
352
                self.registerPopup(site.markers[cache.code], cache.code, siteid);
353
            }
354
355
            for (code in site.markers) {
356
                if (site.markers.hasOwnProperty(code) && addedCaches[code] === undefined) {
357
                    site.markers[code].setMap(null);
358
                    delete site.markers[code];
359
                }
360
            }
361
            site.finished = true;
362
        },
363
        error: function () {
364
            //console.log("okapi request failed: " + site.name);
365
            self.removeMarkersSite(site.markers);
366
            site.markers = {};
367
            site.finished = true;
368
        }
369
    });
370
};
371
372
373
Okapi.loadBbox = function () {
374
    'use strict';
375
376
    if (!this.m_ready) {
377
        return;
378
    }
379
380
    var self = this;
381
    this.m_sites.map(function (site) {
382
        self.loadBboxSite(site.siteid);
383
    });
384
};
385
386
387
Okapi.unscheduleLoad = function () {
388
    'use strict';
389
390
    if (!this.m_ready) {
391
        return;
392
    }
393
394
    if (this.m_timer) {
395
        window.clearTimeout(this.m_timer);
396
        this.m_timer = null;
397
    }
398
};
399
400
401
Okapi.scheduleLoad = function () {
402
    'use strict';
403
404
    if (!this.m_ready) {
405
        return;
406
    }
407
408
    var self = this;
409
410
    this.unscheduleLoad();
411
    this.m_timer = window.setTimeout(function () {
412
        self.loadBbox();
413
    }, 500);
414
};
415
416
417
Okapi.toggle = function (t) {
418
    'use strict';
419
420
    Cookies.set('load_caches', t ? "1" : "0", {expires: 30});
421
    if ($('#geocaches').is(':checked') !== t) {
422
        $('#geocaches').attr('checked', t);
423
    }
424
425
    if (this.m_enabled !== t) {
426
        this.m_enabled = t;
427
    }
428
429
    if (this.m_enabled) {
430
        this.setupIcons();
431
        this.setupSites();
432
        this.scheduleLoad();
433
    } else {
434
        this.unscheduleLoad();
435
        this.removeMarkers();
436
    }
437
};
438
439
440
Okapi.restore = function (defaultValue) {
441
    'use strict';
442
443
    var state = get_cookie_string("load_caches", "invalid");
444
445
    if (state === "0") {
446
        this.toggle(false);
447
    } else if (state === "1") {
448
        this.toggle(true);
449
    } else {
450
        this.toggle(defaultValue);
451
    }
452
};
453